﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web;

/// <summary>
/// ImageCroper 的摘要说明
/// </summary>
public static class ImageCroper
{
    /// <summary>
    ///  保存图片的字符数组到虚拟路径位置
    /// </summary>
    /// <param name="image"></param>
    /// <param name="targetVirtualPath">example:"~/images/as.jpg"</param>
    /// <returns></returns>
    private static bool SaveImage(byte[] image, string targetVirtualPath)
    {
        FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(targetVirtualPath), FileMode.OpenOrCreate);
        fs.Write(image, 0, image.Length);
        fs.Close();
        return true;
    }

    public static bool CropImage(string originaImageVirtualPath, string targetImageVirtualPath, int width, int height, int x, int y)
    {
        byte[] CropImage = Crop(HttpContext.Current.Server.MapPath(originaImageVirtualPath), width, height, x, y);
        if (!SaveImage(CropImage, targetImageVirtualPath))
        {
            return false;
        }
        return true;
    }

    public static bool ChangeWidthAndHeight(string ImageVirtualPath, int width, int height)
    {
        byte[] image;
        try
        {
            using (var OriginalImage = new Bitmap(HttpContext.Current.Server.MapPath(ImageVirtualPath)))
            {
                using (var bmp = new Bitmap(width, height, OriginalImage.PixelFormat))
                {
                    bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                    using (Graphics Graphic = Graphics.FromImage(bmp))
                    {
                        Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                        Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, width, height), 0, 0, OriginalImage.Width, OriginalImage.Height,
                                          GraphicsUnit.Pixel);
                        var ms = new MemoryStream();
                        bmp.Save(ms, OriginalImage.RawFormat);
                        image = ms.GetBuffer();
                        ms.Close();
                    }
                }
            }
        }
        catch (Exception Ex)
        {
            throw (Ex);
        }
        return SaveImage(image, ImageVirtualPath);
    }

    private static byte[] Crop(string Img, int Width, int Height, int X, int Y)
    {
        try
        {
            using (var OriginalImage = new Bitmap(Img))
            {
                using (var bmp = new Bitmap(Width, Height, OriginalImage.PixelFormat))
                {
                    bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                    using (Graphics Graphic = Graphics.FromImage(bmp))
                    {
                        Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                        Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, Width, Height), X, Y, Width, Height,
                                          GraphicsUnit.Pixel);
                        var ms = new MemoryStream();
                        bmp.Save(ms, OriginalImage.RawFormat);
                        return ms.GetBuffer();
                    }
                }
            }
        }
        catch (Exception Ex)
        {
            throw (Ex);
        }
    }
}